
package pri.syl.xiaowu.websocket.core.factory;

import com.google.common.collect.Maps;
import pri.syl.xiaowu.websocket.core.constant.UriFactoryConstant;
import pri.syl.xiaowu.websocket.core.exception.WebsocketException;
import io.netty.util.CharsetUtil;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Map;

/**
 * @Author YunLong
 * @Date 2021/7/18 17:55
 * @Description URL处理工厂
 **/
public class UriFactory {
    private UriFactory() {
    }

    public static boolean containVariable(String route) {
        return route.contains(UriFactoryConstant.OPEN_BRACE);
    }

    /**
     * restful 匹配参数传递类型
     *
     * @param route
     * @param requestUri
     * @return boolean
     * @apiNote [附加描述]
     * @author YunLong
     * @date 2021/7/19 9:18
     */
    public static boolean isMatch(String route, String requestUri) {
        requestUri = conversionRouteUri(requestUri);
        String[] routeUriSplit = route.split(UriFactoryConstant.SLASH);
        String[] requestUriSplit = requestUri.split(UriFactoryConstant.SLASH);
        for (int i = 0; i < routeUriSplit.length; i++) {
            if (routeUriSplit[i].contains(UriFactoryConstant.OPEN_BRACE)) {
                continue;
            }
            if (!routeUriSplit[i].equals(requestUriSplit[i])) {
                return Boolean.FALSE;
            }
        }
        return Boolean.TRUE;

    }

    public static String conversionRouteUri(String requestUri) {
        if (!requestUri.contains(UriFactoryConstant.QUESTION_MARK)) {
            return requestUri;
        }
        int index = requestUri.indexOf(UriFactoryConstant.QUESTION_MARK);
        return requestUri.substring(0, index);
    }

    /**
     * 获取uri的Map传参
     *
     * @param requestUri
     * @return java.util.Map<java.lang.String, java.lang.String>
     * @apiNote [附加描述]
     * @author YunLong
     * @date 2021/7/19 9:20
     */
    public static Map<String, String> getUriParamMap(String requestUri) throws WebsocketException {
        try {
            requestUri = URLDecoder.decode(requestUri, CharsetUtil.UTF_8.toString());
        } catch (UnsupportedEncodingException e) {
            throw new WebsocketException("Uri Decoder Error", e);
        }
        if (!requestUri.contains(UriFactoryConstant.QUESTION_MARK)) {
            return Maps.newConcurrentMap();
        }
        int index = requestUri.indexOf(UriFactoryConstant.QUESTION_MARK);
        String filterUri = requestUri.substring(index + 1);
        String[] splitUri = filterUri.split(UriFactoryConstant.AMPERSAND);
        Map<String, String> paramMap = Maps.newConcurrentMap();
        for (String uriParam : splitUri) {
            int substringIndex = uriParam.indexOf(UriFactoryConstant.IS_EQUAL_TO);
            String key = uriParam.substring(0, substringIndex);
            String value = uriParam.substring(substringIndex + 1);
            paramMap.put(key, value);
        }
        return paramMap;
    }

    /**
     * 获取url restful参数
     *
     * @param routeUri
     * @param clientUri
     * @return java.util.Map<java.lang.String, java.lang.String>
     * @apiNote [附加描述]
     * @author YunLong
     * @date 2021/7/19 9:20
     */
    public static Map<String, String> getUriVariable(String routeUri, String clientUri) {
        clientUri = conversionRouteUri(clientUri);

        Map<String, String> uriVariableMap = Maps.newConcurrentMap();
        String[] routeUriSplit = routeUri.split(UriFactoryConstant.SLASH);
        String[] clientUriSplit = clientUri.split(UriFactoryConstant.SLASH);
        for (int i = 0; i < routeUriSplit.length; i++) {
            String serverUriVariable = routeUriSplit[i];
            if (serverUriVariable.contains(UriFactoryConstant.OPEN_BRACE)) {
                String key = serverUriVariable.substring(1, serverUriVariable.length() - 1);
                String value = clientUriSplit[i];
                uriVariableMap.put(key, value);
            }
        }
        return uriVariableMap;
    }
}
